package cn.itcast.travel.service.impl;

import cn.itcast.travel.dao.*;
import cn.itcast.travel.dao.impl.*;
import cn.itcast.travel.domain.*;
import cn.itcast.travel.service.RouteService;

import java.util.List;

public class RouteServiceImp implements RouteService {
    private RoutDao dao=new RoutDaoImp();
    private RouteImgDao routeImgDao=new RouteImgDaoImp();
    private SellerDao sellerDao=new SellerDaoImp();
    private FavoriteDao favoriteDao=new FavoriteDaoImp();
    private OrderDao od = new OrderDaoImp();
    @Override
    public PageBean<Route> pageQuery(int cid, int currentPage, int pageSize, String rname) {
        PageBean<Route> pageBean=new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        int totalCount = dao.findTotalCount(cid,rname);
        pageBean.setTotalCount(totalCount);
        int start=(currentPage-1)*pageSize;
        List<Route> byPage = dao.findByPage(cid, start, pageSize,rname);
        pageBean.setList(byPage);
        int totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize)+1;
        pageBean.setTotalPage(totalPage);
        return pageBean;
    }

    @Override
    public Route findOne(String rid) {
        int i = Integer.parseInt(rid);
        Route one = dao.findOne(i);
        List<RouteImg> byRid = routeImgDao.findByRid(one.getRid());
        one.setRouteImgList(byRid);
        Seller byId = sellerDao.findById(one.getSid());
        one.setSeller(byId);
        int countByRid = favoriteDao.findCountByRid(one.getRid());
        one.setCount(countByRid);
        return one;
    }

    @Override
    public PageBean<Route> findIndex() {
        PageBean<Route> pageBean=new PageBean<>();
       List<Route>  query=dao.findIndex();
       pageBean.setList(query);
        return pageBean;
    }

    @Override
    public Route findByOut_trade_no(String out_trade_no) {
        //根据订单号查询线路id
        int rid = od.findRidByout_trade_no(out_trade_no);
        //根据线路id查询线路
        Route one = dao.findOne(rid);
        return one;
    }


}
